package com.zw.mes.module.system.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.constant.Constant;
import com.zw.mes.entity.sys.Dict;
import com.zw.mes.module.system.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 字典数据接口
 *
 * 权限：
 * basic:dict:view 查看
 * basic:dict:edit 编辑
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/sys/dict")
@SuppressWarnings("all")
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    @GetMapping
    @RequiresPermissions({"basic:dict:view"})
    public String page(Model model) {
        model.addAttribute("types", Constant.DICT_TYPE_NAME);
        return "system/dict/page";
    }

    /**
     * 系统字典数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"basic:dict:view"})
    public JsonResult list(Dict dict) {
        startPage();
        List<Dict> list = dictService.findDictByCondition(dict);
        return jsonResult(list);
    }

    /**
     * 创建字典数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"basic:dict:edit"})
    public JsonResult save(Dict dict) {
        return isNotNull(dictService.saveDict(dict));
    }

    /**
     * 修改字典数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"basic:dict:edit"})
    public JsonResult update(Dict dict) {
        return isNotNull(dictService.updateDict(dict));
    }

    /**
     * 删除字典数据
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"basic:dict:edit"})
    public JsonResult del(Integer id) {
        return jsonResult(dictService.delDict(id));
    }

}
